/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */

function ListNode(val, next) {
  this.val = (val === undefined ? 0 : val)
  this.next = (next === undefined ? null : next)
}

let l = new ListNode(1, null)
var removeNthFromEnd = function (head, n) {
  // 使用两趟扫描实现
  let p = head
  let len = 0
  while (p) {
    len++
    p = p.next
  }
  let dummyHead = new ListNode()
  let cur = new ListNode()
  dummyHead.next = head
  cur = dummyHead

  for (let i = 0; i < len; i++) {
    if (i == len - n) {
      cur.next = cur.next.next
      break;
    }
    cur = cur.next
  }
  return dummyHead.next
};
removeNthFromEnd(l, 1)